#include "map.h"

#ifdef _NO_WAY_JOSE
    /** Creates a new instance of MapLayer */
  Maplayer maplayer_make(int wide, int high)  {
        self = eruta_make(Maplayer);
        self->wide          = wide;
        self->high          = high;
        self->tilewide      = 32;
        self->tilehigh      = 32;
        // self->_map          = eruta_allocate(sizeof(SDL_Surface *
        /* for(int index = 0; index < _wide; index ++) {
            _images[index] = new Image[_wide];
        }*/
    }
    
    public int getWide() {
        return _wide;
    }
    
    public int getHigh() {
        return _high;
    }
    
    public void set(Image image, int x, int y) {
        _images[x][y] = image;
    }
    
    public void setAll(Image image) {
        int x; int y;
        for(x = 0; x < _wide; x++) {
            for(y = 0; y < _high; y++) {
                _images[x][y] = image;
            }    
        }
    }
    
    
    
    public void prerender(org.eruta.inout.Screen screen) {
        _render = screen.accelerate(null, _wide * _tilewide, _high * _tilehigh, 
                java.awt.Transparency.OPAQUE, 0, 0);
        
        
        int txstart = 0;
        int tystart = 0;
        
        int txstop = _wide;
        int tystop = _high;
        int x = 0;
        int y = 0;
        int drawx  = 0;
        int drawy  = 0;
        Image row[];
        Image aidi = null;
               
        long calctotal = 0;
        long drawtotal = 0;
        long calcbefore;
        long drawbefore;
        long calcafter;
        long drawafter;
        drawx = -x + ( (txstart-1) * _tilewide );
        //System.gc();
        Graphics2D target = (Graphics2D) _render.getGraphics();
        int tydex = 0;
        int txdex = 0;

        for (txdex = txstart; txdex < txstop; txdex ++ ) {
            drawx   += _tilewide;            
            drawy   = -y + ( (tystart-1) * _tilehigh );
            row     = _images[txdex];            
            if(row == null) continue;
            
            for (tydex = tystart; tydex < tystop; tydex ++ ) {                
                aidi   = row[tydex];                
                if(aidi == null) continue;
                // Get the tile. If it is null, immediately skip to the next tile.
                calcbefore = System.nanoTime();
                drawy   += _tilehigh;
                
                calcafter = System.nanoTime();                               
                // The tile rendered can never be completely off screen
                drawbefore = System.nanoTime();
                target.drawImage(aidi, drawx, drawy, null);
                drawafter = System.nanoTime();                
                drawtotal += (drawafter - drawbefore);
                calctotal += (calcafter - calcbefore);
            }
        
        }
        System.out.format("Map prerender calc/draw: %d ms, %d ms: %d, %d.\n", 
                            calctotal / 1000000, drawtotal / 1000000, txdex, tydex);
    
    }
    
    
    public void drawpre(Graphics2D target, int x, int y) {
        target.drawImage(_render, -x, -y, null);
    }
    
    
    public void draw(Graphics2D target, int x, int y) {
        
        
        int txstart = ( x / _tilewide );
        int tystart = ( y / _tilehigh );
        if (txstart >= _wide || tystart >= _high) { 
            return;
        } 
        if (txstart < 0) txstart = 0;
        if (tystart < 0) tystart = 0;
        int txstop = _xtilestop + txstart;
        int tystop = _ytilestop + tystart;
        if  (txstop >= _wide) txstop = _wide;
        if  (tystop >= _high) tystop = _high;        
        int drawx  = 0;
        int drawy  = 0;
        Image row[];
        Image aidi = null;
        
        
        long calctotal = 0;
        long drawtotal = 0;
        long calcbefore;
        long drawbefore;
        long calcafter;
        long drawafter;
        drawx = -x + ( (txstart-1) * _tilewide );
        //System.gc();
       

        for (int txdex = txstart; txdex < txstop; txdex ++ ) {
            drawx   += _tilewide;            
            drawy   = -y + ( (tystart-1) * _tilehigh );
            row     = _images[txdex];            
            if(row == null) continue;
            
            for (int tydex = tystart; tydex < tystop; tydex ++ ) {                
                aidi   = row[tydex];                
                if(aidi == null) continue;
                // Get the tile. If it is null, immediately skip to the next tile.
                calcbefore = System.nanoTime();
                drawy   += _tilehigh;
                
                calcafter = System.nanoTime();                               
                // The tile rendered can never be completely off screen
                drawbefore = System.nanoTime();
                target.drawImage(aidi, drawx, drawy, null);
                drawafter = System.nanoTime();                
                drawtotal += (drawafter - drawbefore);
                calctotal += (calcafter - calcbefore);
            }
        
        }
        System.out.format("Map draw calc/draw: %d, %d.\n", 
                            calctotal / 1000, drawtotal / 1000);
    
    }
#endif


